
import * as mUtils from '@/utils/mUtils'
import { logout, getUserInfo, INfOuserList } from '@/api/user'  // 导入用户信息相关接口
import { getToken, setToken, removeToken } from '@/utils/auth'

import router from '../../router';

const user = {
  state: {
    branch_id: '',
    name: '',
    avatar: '',
    token: getToken('Token'),
    roles: [],
    browserHeaderTitle: mUtils.getStore('browserHeaderTitle') || '渡石医药供应链',
    user_id: '',
    nickname: '',
    arrList: [],
    feedback: '',
  },
  getters: {
    branch_id: state => state.branch_id,
    token: state => state.token,
    roles: state => state.roles,
    avatar: state => state.avatar,
    name: state => state.name,
    browserHeaderTitle: state => state.browserHeaderTitle,
    user_id: state => state.user_id,
    nickname: state => state.nickname,
    arrList: state => state.arrList,
    feedback: state => state.feedback,
    setting_list: state => state.setting_list,
  },
  mutations: {
    SET_BRANCH_ID: (state, id) => {
      state.branch_id = id
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_BROWSERHEADERTITLE: (state, action) => {
      state.browserHeaderTitle = action.browserHeaderTitle
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_USER_ID: (state, id) => {
      state.user_id = id
    },
    SET_NICKNAME: (state, nickname) => {
      state.nickname = nickname
    },
    SET_CONTROL: (state, arrList) => {
      state.arrList = arrList
    },
    FEED_BACK: (state, feedback) => {
      state.feedback = feedback
    },
    SETTING_LIST: (state, setting_list) => {
      state.setting_list = setting_list
    }
  },
  actions: {
    //登出
    LogOut({ commit, reqData }) {
      return new Promise((resolve, reject) => {
        logout(reqData).then(response => {
          commit('SET_ROLES', [])
          removeToken('Token')
          removeToken('Cookie')
          localStorage.removeItem('user')
          localStorage.removeItem('allUserInfo')
          resolve()
        })
      })
    },
    // 动态修改权限;本实例中,role和token是相同的;
    ChangeRoles({ commit }, role) {
      return new Promise(resolve => {
        const token = role;
        setToken("Token", token)
        getUserInfo({ "token": token }).then(res => {
          let data = res.data.userList;
          commit('SET_ROLES', data.roles)
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          resolve()
        })
      })
    },
    setUserInfo({ commit, dispatch }, params) {
      INfOuserList(params).then(res => { // 根据token拉取用户信息
        let userList = res.data || {}
        setToken("Token", userList.token);

        commit("SET_BRANCH_ID", userList.branch_id);
        commit("SET_ROLES", userList.username);
        commit("SET_NAME", userList.username);
        commit("SET_AVATAR", userList.profile_photo);
        commit("SET_USER_ID", userList.user_id);
        commit("SET_NICKNAME", userList.nickname);
        dispatch('GenerateRoutes', { "menu": userList.menu }).then(() => { // 根据roles权限生成可访问的路由表
          // router.addRoutes(store.getters.addRouters) // 动态添加可访问权限路由表
          // next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
        })
        localStorage.setItem('user', JSON.stringify({
          saas_company_id: userList.saas_company_id,
          user_id: userList.user_id,
          branch_id: userList.branch_id,
          branch_name: userList.branch_name,
          nickname: userList.nickname,
          mobile: userList.mobile,
          role_data: userList.role_data,
          type: userList.type,
          type_id: userList.type_id,
          button_name_list: userList.button_name_list,
          product_service_version: userList.product_service_version,
          product_type_info: userList.product_type_info,
        }))
        localStorage.setItem(
          "user_id",
          userList.user_id
        );
        // location.reload();
        // setTimeout(()=>{
        //   router.push({path: '/index/index'})
        // },1000)
        let type= (JSON.parse(localStorage.getItem("user"))||{}).type;
        if(type===2||type===3){
          window.location.replace('/permission/infoManage/userCenter');
        }else{
          window.location.replace('/permission/index/index');
        }
      }).catch((err) => {
        store.dispatch('LogOut').then(() => {
          Message.error(err || 'Verification failed, please login again')
          next({ path: '/' })
        })
      })
    }
  }
}

export default user;

/**
 * 1、用户退出,需要调取后台接口吗？后台具体的业务逻辑是什么？
 * 
 * 
 */